<?php 
	session_start();
	require "functions.php";
	if (isUserLoged()) {
		header("Location: index.php");
		die();
	}
?>
<html>
<head>
	<title>Sign Up</title>
	<link rel="stylesheet" type="text/css" href="style/style.css">
</head>
<body>
<div id="wrapper">
	<div id="register">
		<form method="post" action="register.php">
			<p>Username: <input type="text" name="username" /></p>
			<?php 
				if($_POST)
				{
					$isUsernameValid = validateUsername($_POST['username']);
					if(!$isUsernameValid)
					{
						echo "<p class=\"warning\">Please enter valid username!</p>";
					}
				}
			?>
			<p>Password: <input type="password" name="password" /></p>
			<?php 
				if($_POST)
				{
					$isPasswordValid = validatePassword($_POST['password']);
					if(!$isPasswordValid)
					{
						echo "<p class=\"warning\">Please enter valid password!</p>";
					}
				}
			?>
			<p>Money: <input type="text" name="money" /></p>
			<?php 
				if($_POST)
				{
					$isMoneyValid = validateMoney($_POST['money']);
					if(!$isMoneyValid)
					{
						echo "<p class=\"warning\">Please enter valid money balance!</p>";
					}
				}
			?>
			<p><input type="submit" name="submit" value="Sign Up" /></p>
		</form>
		<?php 		
		if($_POST)
		{
			if($isUsernameValid && $isPasswordValid && $isMoneyValid)
			{
				if(isUsernameFree($_POST['username']))
				{
					insertUser($_POST['username'], $_POST['password'], $_POST['money']);			
					header("Location: index.php");
				}
				else 
				{
					echo "<p class=\"warning\">This usrname is taken, please try again!</p>";
				}
			}
		}
		?>
		<p class="small">Username and password must be between 6 and 30 symbols. Allowed only 
		   small letters and number.</p>
		<p>Have an account? <a href="login.php">Login!</a></p>
	</div>
</div>
</body>
</html>